package com.zfjt.controller;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSON;
import com.zfjt.constants.InterfaceDefinition;
import com.zfjt.entity.ReturnBean;
import com.zfjt.entity.UserBean;
import com.zfjt.packnum.PackNum00028;
import com.zfjt.service.UserService;

/**
 * 修改个人头像
 * @author xk
 * @version 1.0
 * @date 2017年4月11日
 */
@Controller
public class RevampHeadController {
	
	private ReturnBean mReturn;
	
	@Autowired
	private UserService mUser;
	
	private Map<String, String> fhdata;
	
	@RequestMapping(value="00028")
	@ResponseBody
	public ReturnBean RevampHead(HttpSession hseesion,HttpServletRequest request){
		mReturn = new ReturnBean();
		fhdata = new HashMap<>();
		//传过来的参数
		String data =  (String) hseesion.getAttribute("data");
		PackNum00028 num00028 = JSON.parseObject(data,PackNum00028.class); 		
		if (data.equals(InterfaceDefinition.IStatusCode.TOKEN_FAILURE)) {//校验码错误
			mReturn.setCode("001");
			mReturn.setPortnum("00028");
			fhdata.put("error", "TOKEN_ERROR");
			String fh = JSON.toJSONString(fhdata);
			mReturn.setData(fh);
		}else{
			UserBean user = mUser.selectByPrimaryKey(num00028.getAccount());
			//获取Tomcat下项目根目录
			String path = request.getSession().getServletContext().getRealPath("images" + "/") +"head";
			//根据用户名新建文件夹存相应照片
			File file = new File(path);
			if (!file.exists()) {
				file.mkdirs();
			}
			String path2 = path + num00028.getDate() + ".jpg";
			File file2 = new File(path2);
			file2.mkdirs();
			user.setHead(path2);
			mUser.updateByPrimaryKeySelective(user);
			mReturn.setCode("000");
			mReturn.setPortnum("00028");
			mReturn.setData("chenggong");
		}
		return mReturn;
	}
}
